//
//  UIViewController+STNavigationTools.m
//  SportHome
//
//  Created by stoneobs on 17/1/11.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

#import "UIViewController+STNavigationTools.h"

@implementation UIViewController (STNavigationTools)
#pragma mark ----导航栏相关配置
- (void)setLeftItemWithTitle:(NSString *)title
{
    UIBarButtonItem * left = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction:)];
    self.navigationItem.leftBarButtonItem = left;
}
- (void)setLeftItemWithTitle:(NSString *)title titleColor:(UIColor*)color;
{

    UIBarButtonItem * left =[[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction:)];
    [left setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil] forState:UIControlStateNormal];
    left.tintColor = color;
    
    self.navigationItem.leftBarButtonItem = left;
}
- (void)setLeftItemWithImage:(UIImage *)image
{
    UIBarButtonItem * leftImage = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction:)];
    self.navigationItem.leftBarButtonItem = leftImage;
}
- (void)setLeftItemWithImage:(UIImage *)image andwithTitle:(NSString *)title
{
    UIButton * but =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 60)];
    [but setImage:image forState:UIControlStateNormal];
    [but setTitle:title forState:UIControlStateNormal];
    [but setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    but.contentEdgeInsets=UIEdgeInsetsMake(0, -20, 0, 25);
    [but addTarget:self action:@selector(leftBarAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * left = [[UIBarButtonItem alloc]initWithCustomView:but];
    
    self.navigationItem.leftBarButtonItem = left;
}
- (void)setRightItemWithTitle:(NSString *)title
{
    UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction:)];
    right.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = right;
}
- (void)setRightItemWithTitle:(NSString *)title titleColor:(UIColor*)color
{
    
    UIBarButtonItem * right =[[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction:)];
    [right setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil] forState:UIControlStateNormal];
    right.tintColor = color;
    
    self.navigationItem.rightBarButtonItem = right;
}
- (void)setRightItemWithImage:(UIImage *)image
{
    UIImage * editimgae = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem * rightImage =[[UIBarButtonItem alloc]initWithImage:editimgae style:UIBarButtonItemStyleDone target:self action:@selector(rightBarAction:)];
    self.navigationItem.rightBarButtonItem=rightImage;
}
- (void)setRightItemWithImage:(UIImage *)image andwithTitle:(NSString *)title
{
    UIButton * but = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 60)];
    [but setImage:image forState:UIControlStateNormal];
    [but setTitle:title forState:UIControlStateNormal];
    [but setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //but.contentEdgeInsets=UIEdgeInsetsMake(0, -20, 0, 25);
    [but addTarget:self action:@selector(rightBarAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithCustomView:but];
    self.navigationItem.rightBarButtonItem = right;
    
}
- (void)setLeftItemWithView:(UIView *)view
{
    UIBarButtonItem * left =[[UIBarButtonItem alloc]initWithCustomView:view];
    UITapGestureRecognizer * tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(leftBarAction:)];
    [view addGestureRecognizer:tap];
    self.navigationItem.leftBarButtonItem=left;
    
}

- (void)setRightItemWithView:(UIView *)view
{
    UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithCustomView:view];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rightBarAction:)];
    [view addGestureRecognizer:tap];
    self.navigationItem.rightBarButtonItem = right;
    
}
- (void)rightBarAction:(id)sender
{
    
}
- (void)leftBarAction:(id)sender
{
    //[self.navigationController popViewControllerAnimated:YES];
}
-(void)showNavagationbar
{
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

-(void)hideNavagetionbar
{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

@end
